﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.Commons;
using SD.DataAccess;
using System.Globalization;

namespace SD.Web.SupportingMaterials
{
    public partial class AddSupportingMaterials : System.Web.UI.Page
    {
        /// <summary>
        /// Get and set Id of Organization
        /// </summary>
        public int OrganizationId
        {
            get
            {
                if (ViewState["OrganizationId"] != null)
                {
                    return (int)ViewState["OrganizationId"];
                }
                return -1;
            }
            set
            {
                ViewState["OrganizationId"] = value;
            }
        }
        /// <summary>
        /// Handle event Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) 
            {
                BindDropDownListType();
                BindFields();
            }
        }
        ///[TrungL1]: Add comment to method BindField. 9:00PM 25-Dec-2012
        /// <summary>
        /// Method Bind Data to Field in form
        /// </summary>
        private void BindFields()
        {
            int organizationId;
            if (Int32.TryParse(Request.Params["organizationId"], out organizationId))
            {
                if (HOrganizationBO.GetOrganizationById(organizationId) != null)
                {
                    OrganizationId = organizationId;
                    ButtonSave.Enabled = true;
                    ButtonBack.Enabled = true;
                    TextBoxAddedBy.Text = Session["User"].ToString();
                    TextBoxAddedDate.Text = DateTime.Now.ToShortDateString();
                }
            }
        }
        //////[TrungL1]: Add comment to method BindDropDownListType. 9:00PM 25-Dec-2012
        /// <summary>
        /// Bind data to Drop down list
        /// </summary>
        private void BindDropDownListType()
        {
            DropDownListType.DataSource = DocumentUtilities.SupportingMaterialTypes;
            DropDownListType.DataBind();
            DropDownListType.Items.Insert(0, new ListItem("None", string.Empty));
        }
        /// <summary>
        /// Handle event click button Save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            bool result = false;
            SupportingMaterial supportingMaterial = GetSupportingMaterial();
            if (supportingMaterial != null)
            {
                result = SupportingMaterialBus.InsertSupportingMaterial(supportingMaterial);
                if (result)
                {
                    Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Views/Organization/OrganizationDetail.aspx?id={0}#DetailsPanel05",
                                                    OrganizationId));
                    return;
                }
                else 
                {
                    DialogBox.Error("Can not insert Supporting Material!");
                }
            }
        }
        /// <summary>
        /// Method GetSupportingMaterial
        /// </summary>
        /// <returns></returns>
        private SupportingMaterial GetSupportingMaterial()
        {
            if (Session["userId"] != null)
            {
                var supportingMaterial = new SupportingMaterial();
                supportingMaterial.Url = TextBoxURL.Text;
                supportingMaterial.Description = TextBoxDescription.Text;
                supportingMaterial.Type = DropDownListType.SelectedValue;
                supportingMaterial.UserId = (int)Session["userId"];
                supportingMaterial.AddedDate = DateTime.Now;
                supportingMaterial.IsActive = true;
                supportingMaterial.OrganizationId = OrganizationId;
                return supportingMaterial;
            }
            return null;
        }
        /// <summary>
        /// Handle event click button Back
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Views/Orgranization/OrganizationList.aspx",
                                            OrganizationId));
        }
    }
}